package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.AdminService;
import service.UserService;

/**
 * Servlet implementation class AdminLogin
 */
@WebServlet("/AdminLogin")
public class AdminLogin extends HttpServlet {
	/*private static final long serialVersionUID = 1L;*/
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		//String username,password;
		AdminService as =new AdminService();
		HttpSession ss = request.getSession();
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		String b = as.login(username, password);
		if(b!=null){
			//request.getRequestDispatcher("../add_movie.jsp").forward(request, response);
			response.sendRedirect("../add_movie.jsp");
			ss.setAttribute("admin", b);
		}
		else{
		response.setCharacterEncoding("gbk");
		PrintWriter out = response.getWriter();
		//out.println("<%@ page language="java" import='java.util.*' pageEncoding='utf-8' %> ");
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>登录失败</TITLE></HEAD>");
		out.println("  <BODY>");
        out.println("<h1>帐号或用户名错误!</h1>");
        out.println(b);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
		}
	}

}
